<template>
	<view class="" style="padding-bottom: 150rpx;">
		<u-tabs :list="tabList" :scrollable="false" @click="tabchange" lineWidth="60rpx"
			lineColor="rgba(255, 184, 0, 1)" :activeStyle="{
		  color: '#000000',
		  fontWeight: 800,
		  transform: 'scale(1.05)'}" :inactiveStyle="{
		  color: '#666666',
		    transform: 'scale(1)'
		}"></u-tabs>
		<view class="mainpadding2">
			<template v-if="tabsid==0">
				<view class="chatItemMessage margin_top2" v-if="item.new_content" v-for="item in chatList" :key="item.id" @click="enterchat(item)">
					<view class="imageBox">
						<image :src="item.firend.avatar_text || item.firend.avatar" mode="">
						</image>
					</view>
					<view class="right">
						<view class="nameTime">
							<view class="flexleft">
								<view class="name xiankuan">{{item.firend.shop.name}}</view>
								<view class="number" v-if="item.noread_num">{{item.noread_num}}</view>
							</view>
							<view class="time" v-if="item.new_content">{{item.new_content.createtime_text}}</view>
						</view>
						<template v-if="item.new_content">
							<template v-if="item.new_content.msg_status==1">
								<view class="bottom yihang" v-if="item.new_content.data_type==1">{{item.new_content.message}}</view>
								<view class="bottom yihang" v-if="item.new_content.data_type==2">[圖片]</view>
								<view class="bottom yihang" v-if="item.new_content.data_type==3">[影片]</view>
							</template>
							<template v-if="item.new_content.msg_status==2">
								<view class="bottom yihang">{{item.new_content.sender_id=='user'+user_id?'你':'对方'}}撤回了一條消息</view>
							</template>
						</template>
					</view>
				</view>
			</template>
			<template v-if="tabsid==1 || tabsid==2">
				<view class="mainpadding_top ffffff border2" v-for="item in list.data" :key="item.id"
					@click="jumpdetail('/pages_message/noticeDetail?id=',item.id)">
					<view class="flexbetween">
						<view class="stateBox text_e radius color2">{{item.title}}</view>
						<view class="color_six text_five margin_top2">{{item.createtime_text}}</view>
					</view>
					<view class="margin_top color_ling text_a padding_bottom border_bottom">
						{{item.des}}
					</view>
					<view class="flexright margin_top2">
						<view class="lanlable">看詳情</view>
					</view>
				</view>
			</template>
		</view>
		
		<tabbar ref="tabbarref" :current="4"></tabbar>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				user_id:"",
				chatList:[],
				huodongAll:{},
				gonggaoAll:{},
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				tabsid: 0,
				tabList: [{
					name: "訊息",
					id: 0,
				},{
					name: "公告",
					id: 1,
				}, {
					name: "活動",
					id: 2,
				}],
			}
		},
		onLoad() {
			uni.hideTabBar()
			uni.$on("updatechatFnList", data => {
				console.log("触發方法了會话列表哈哈哈");
				this.initchat()
			})
		},
		onShow() {
			this.$nextTick(() => {
				this.$refs.tabbarref.init()
			});
			if (httpRequest.checkIsLogin()) {
				this.user_id = uni.getStorageSync("user_id")
				this.initchat()
			}
			this.getptnews()
		},
		onUnload() {
			uni.$off('updatechatFnList')
		},
		methods: {
			init(isPage, page) {
				let _this = this
				let url = "/api/index/ptnewsIndex"
				let data = {
					page: page || 1,
					limit: _this.limit,
					type: this.tabsid
				}
				if(this.tabsid==0) return
				httpRequest.request(url, 'GET', data).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			tabchange(e) {
				this.tabsid = e.id
				this.init()
				this.list.data = []
			},
			getptnews() {
				httpRequest.request('/api/index/ptnewsIndex', 'GET', {
					page:1,
					limit:1,
					type:1
				}).then(res => {
					this.gonggaoAll = res.data.data
				})
				httpRequest.request('/api/index/ptnewsIndex', 'GET', {
					page:1,
					limit:1,
					type:2
				}).then(res => {
					this.huodongAll = res.data.data
				})
			},
			initchat() {
				httpRequest.request('/api/user/getChatList', 'GET', {
					user_id:uni.getStorageSync("user_id"),
					sendtype:1
				}).then(res => {
					this.chatList = res.data
				})
			},
			goSystemNotice(type){ //系統通知
				uni.navigateTo({
					url:"/pages_message/systemNotice?type="+type
				})
			},
			goOrderNotice(){ //訂單提醒
				uni.navigateTo({
					url:"/pages_message/orderNotice"
				})
			},
			// 進入聊天
			enterchat(item) { //進入聊天
				let data = {
					firend_id: item.firend.id,
					friend_name: item.firend.shop.name,
					firend_avatar_text: this.imgUrl + item.firend.shop.logoimage
				}
				uni.navigateTo({
					url: "/pages_message/chat?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.imgBox {
		width: 80rpx;
		height: 80rpx;
	}
	.border_bottom{
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.03);
	}
	.chatBox {
		background-color: #FAFAFA;
		padding: 70rpx 40rpx 0;
	}
	
	.chatItemMessage {
		display: flex;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid rgba(0, 0, 0, .08);
		.imageBox {
			width: 80rpx;
			min-width: 80rpx;
			height: 80rpx;
			border-radius: 10rpx;
			overflow: hidden;
			margin-right: 20rpx;
	
			image {
				width: 100%;
				height: 100%;
			}
		}
	
		.right {
			flex: 1;
		}
	
		.nameTime {
			display: flex;
			justify-content: space-between;
	
			.name {
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #000000;
				margin-right: 10rpx;
			}
	
			.number {
				width: 34rpx;
				height: 34rpx;
				line-height: 34rpx;
				text-align: center;
				background: #FF526A;
				border-radius: 50%;
				font-size: 20rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
			}
	
			.time {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333;
			}
		}
	
		.bottom {
			font-size: 26rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333;
		}
	}
	.stateBox {
		padding: 5rpx 15rpx;
	}
	
	.color2 {
		color: #fff;
		background: #9064ff;
	}
</style>